//
//  UserCenterMenuButton.m
//  xpkc
//
//  Created by TopSage on 15/10/16.
//  Copyright (c) 2015年 TopSage. All rights reserved.
//

#import "UserCenterMenuButton.h"
#import "UIView+Frame.h"
@interface UserCenterMenuButton (){
    UILabel *badgeValueLabel;
}

@end

@implementation UserCenterMenuButton

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self creatMenuView];
    }
    return self;
}

- (void)creatMenuView{
    
    CGFloat height = self.height;
    CGFloat offset = 0.04*SCREEN_WIDTH;
    CGFloat imgw   = 0.08*SCREEN_WIDTH;
    
    CGRect frame_btnimg;
    CGRect frame_btntitle;
    CGRect frame_badgevalue;
    CGRect frame_right;
    CGRect frame_line;
    if (isPad) {
        //FIXME:pad适配另作
        frame_btnimg     =  CGRectMake(offset, (height-imgw)/2+17, imgw/2, imgw/2);
        frame_btntitle   =  CGRectMake(offset*1.5+imgw, (height-imgw)/2, SCREEN_WIDTH/2, imgw);
        frame_badgevalue =  CGRectMake(SCREEN_WIDTH - offset*5, (height-offset)/2, offset, offset);
        frame_right      =  CGRectMake(SCREEN_WIDTH - offset*1.5, (height-offset)/2, offset/2, offset);
        frame_line       =  CGRectMake(0, height-1, SCREEN_WIDTH, 1);
    }else{
        frame_btnimg     =  CGRectMake(offset, (height-imgw)/2, imgw, imgw);
        frame_btntitle   =  CGRectMake(offset*1.5+imgw, (height-imgw)/2, SCREEN_WIDTH/2, imgw);
        frame_badgevalue =  CGRectMake(SCREEN_WIDTH - offset*5, (height-offset)/2, offset, offset);
        frame_right      =  CGRectMake(SCREEN_WIDTH - offset*1.5, (height-offset)/2, offset/2, offset);
        frame_line       =  CGRectMake(0, height-1, SCREEN_WIDTH, 1);
    }
    
    
    self.buttonImage.frame = frame_btnimg;
    
    self.buttonTitle.frame = frame_btntitle;
    self.buttonTitle.font = [UIFont systemFontOfSize:16];
    self.buttonTitle.textColor = [UIColor blackColor];
    
    badgeValueLabel = [[UILabel alloc]initWithFrame:frame_badgevalue];
    badgeValueLabel.hidden = YES;
    badgeValueLabel.backgroundColor = [UIColor redColor];
    badgeValueLabel.textColor = [UIColor whiteColor];
    badgeValueLabel.font = [UIFont systemFontOfSize:14];
    badgeValueLabel.layer.cornerRadius = frame_badgevalue.size.height/2;
    badgeValueLabel.clipsToBounds = YES;
    badgeValueLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:badgeValueLabel];
    
    UIImageView *right = [[UIImageView alloc]initWithFrame:frame_right];
    right.image = [UIImage imageNamed:@"my_rightarrow"];
    [self addSubview:right];
    
    UIView *line = [[UIView alloc]initWithFrame:frame_line];
    line.backgroundColor = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:0.3];
    [self addSubview:line];
}

- (void)setBadgeValue:(NSString *)badgeValue{
    if (badgeValue.length > 0) {
        badgeValueLabel.hidden = NO;
        badgeValueLabel.text = badgeValue;
        if (badgeValue.length >= 2) {
            badgeValueLabel.width = 0.06*SCREEN_WIDTH;
        }else{
            badgeValueLabel.width = 0.04*SCREEN_WIDTH;
        }
    }else{
        badgeValueLabel.hidden = YES;
        badgeValueLabel.text = badgeValue;
    }
}

@end
